#!/bin/sh
# -*- coding: utf-8 -*-
#Filename:   make.sh
#Author:     Yaoyi
#Email:      yaoyi@zzvcom.com
#Date:       2019-06-12 11:47:52
#Desc:       

#load func
source ./source/common/functions.sh
IFS=$'\n'
export LANG="en_CN.gbk"
#set var
NAME=`basename $0`
dir=`pwd`
updatepath=/opt
svnusername=renyumin
svnpwd=ryumin
conffile=${dir}/conf/DIRCONFIG.ini
packagename=`readIniSections ${conffile} PackageName Upgrade|grep -v "^$"|grep -v "^#"`
packagename=`eval echo ${packagename}`
sys=`echo ${packagename}|awk -F "[-_]" '{print $2}'`
svnpath=`readIniSections ${conffile} SvnPath END|grep -v "^$"|grep -v "^#"`
checkconf=${dir}/check/conf/check_1.ini
#delete old tmp
rm -rf ${dir}/check/conf/*list
rm -rf ${dir}/source/package/*
for shell in backuped  before_backup  before_boot  before_update  booted  updated
do
	rm -rf ${dir}/source/${shell}/*
done

##download updatefile
cd ${dir}/source/package
for path in ${svnpath}
do
	svn export "${path}" --username=${svnusername} --password=${svnpwd}
	[ $? -nq 0 ] && (yaoecho hr "svn doenload error";EchoErr ${LINENO};exit 1)
	if [ `basename ${path}|grep -E "*\.tgz"` ]
	then
		tar xf `basename ${path}`
		rm -rf `basename ${path}`
	fi
	if [ `basename ${path}|grep -E "*\.zip"`]
	then
		unzip `basename ${path}`
		rm -rf `basename ${path}`
	fi
done
cd ${dir}

UpdateFile=`ls ${dir}/source/package`
UpConfFile=`readIniSections ${conffile} Upgrade Service|grep -v "^$"|grep -v "^#"|cut -d "=" -f1`
if ! [ X"${UpConfFile}" = X ]
then
    for updatefile in ${UpdateFile}
    do
        checkresult=`echo ${UpConfFile}|grep ${updatefile}`
        if [[ X"${checkresult}" = X ]]
        then
            yaoecho hr "source/package/${updatefile} not in ${conffile},please check it in [Upgrade]"
            EchoErr ${LINENO}
            exit 1
        fi
        echo "${updatefile}=${updatepath};"
    done
    sed -n '/\[Upgrade\]/,/\[END\]/p' ${conffile} |grep -v "^#" > ${dir}/source/conf/DIRCONFIG.ini
else
    echo "[Upgrade]" > ${dir}/source/conf/DIRCONFIG.ini
    echo "[Service]" >> ${dir}/source/conf/DIRCONFIG.ini
    readIniSections ${conffile} Service Process|grep -v "^$"|grep -v "^#" >> ${dir}/source/conf/DIRCONFIG.ini
    echo "[Process]" >> ${dir}/source/conf/DIRCONFIG.ini
    readIniSections ${conffile} Process SvnPath|grep -v "^$"|grep -v "^#" >> ${dir}/source/conf/DIRCONFIG.ini
    echo "[SvnPath]" >> ${dir}/source/conf/DIRCONFIG.ini
    readIniSections ${conffile} SvnPath END|grep -v "^$"|grep -v "^#" >> ${dir}/source/conf/DIRCONFIG.ini
    echo "[END]" >> ${dir}/source/conf/DIRCONFIG.ini
    for updatefile in ${UpdateFile}
    do
        sed -i '/\[Upgrade\]/a '"${updatefile}=${updatepath};${updatefile}/upload;"'' ${dir}/source/conf/DIRCONFIG.ini
    done
fi

# create new package
## 1.read conf file and write to source/conf/DIRCONFIG.ini
sed -n '/\[Upgrade\]/,/\[END\]/p' ${conffile} |grep -v ^# > ${dir}/source/conf/DIRCONFIG.ini
## 2.make md5
yaoecho hv "begin to make md5"
for programe in `readIniSections "${dir}/source/conf/DIRCONFIG.ini" Upgrade Service|grep -v "^$"|grep -v "^#"|cut -d "=" -f1`
do
	echo "${programe}=${dir}/source/package" > ${dir}/md5/DIRCONFIG
	cd ${dir}/md5
	sh produce.sh
	cd ${dir}/source/package
	tar zcvf ${programe}.tgz ${programe}
	rm -rf ${programe}
	rm -rf ${dir}/md5/DIRCONFIG
done
yaoecho hg "make md5 finished"
## 3.config checkconf
sed -i '/platform_type/,/process_normal/{//!d}' ${checkconf}
sed -i '/platform_type/a\'${sys}'' ${checkconf}
#sed -i '/process_java/,/file_exist/{//!d}' ${checkconf}
sed -i '/md5_config/,/ip_config/{//!d}' ${checkconf}
for md5conf in `readIniSections "${dir}/source/conf/DIRCONFIG.ini" Upgrade Service|grep -v "^$"|grep -v "^#"|cut -d ";" -f1`
do
	sed -i '/md5_config/a\'${md5conf}'' ${checkconf}
done
## 4. create new package directory and copy
yaoecho ""
yaoecho hr "Upgrading package"
yaoecho hy -n "Upgrade package name :"
yaoecho hw "\t${packagename}"
mkdir -p ${dir}/product/${packagename}
cd ${dir}
#extglob on
shopt -s extglob
cp -r !(make.sh|product|conf) ${dir}/product/${packagename}
#extglob off
shopt -u extglob
yaoecho hi "Upgrade package directory is product"


